package com.gxg.dao;

import com.gxg.entities.NodeGroup;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class NodeGroupDao
{
  @Autowired
  private JdbcTemplate jdbcTemplate;
  
  public void deleteAll()
  {
    String sql = "delete from NodeGroup";
    this.jdbcTemplate.update(sql);
  }
  
  public int getCountByGroupNumber(int groupNumber)
  {
    String sql = "select count(*) from NodeGroup where groupNumber=?";
    int rowCount = ((Integer)this.jdbcTemplate.queryForObject(sql, Integer.class, new Object[] { Integer.valueOf(groupNumber) })).intValue();
    return rowCount;
  }
  
  public void insertByGroupNumber(int groupNumber)
  {
    String sql = "insert into NodeGroup values(?, null, null)";
    this.jdbcTemplate.update(sql, new Object[] { Integer.valueOf(groupNumber) });
  }
  
  public int getCountByUserIp(String userIp)
  {
    String sql = "select count(*) from NodeGroup where userIp=?";
    int rowCount = ((Integer)this.jdbcTemplate.queryForObject(sql, Integer.class, new Object[] { userIp })).intValue();
    return rowCount;
  }
  
  public NodeGroup getNodeGroupByUserIp(String userIp)
  {
    String sql = "select * from NodeGroup where userIp=?";
    NodeGroup nodeGroup = (NodeGroup)this.jdbcTemplate.queryForObject(sql, new RowMapper()
    {
      public NodeGroup mapRow(ResultSet resultSet, int i)
        throws SQLException
      {
        NodeGroup nodeGroup = new NodeGroup();
        nodeGroup.setGroupNumber(resultSet.getInt("groupNumber"));
        nodeGroup.setUserIp(resultSet.getString("userIp"));
        nodeGroup.setTime(resultSet.getTimestamp("time"));
        return nodeGroup;
      }
    }, new Object[] { userIp });
    
    return nodeGroup;
  }
  
  public int getCountByUserIpIsNull()
  {
    String sql = "select count(*) from NodeGroup where userIp is null";
    int rowCount = ((Integer)this.jdbcTemplate.queryForObject(sql, Integer.class)).intValue();
    return rowCount;
  }
  
  public List<NodeGroup> getNodeGroupByUserIpIsNull()
  {
    String sql = "select * from NodeGroup where userIp is null";
    List<NodeGroup> nodeGroupList = this.jdbcTemplate.query(sql, new RowMapper()
    {
      public NodeGroup mapRow(ResultSet resultSet, int i)
        throws SQLException
      {
        NodeGroup nodeGroup = new NodeGroup();
        nodeGroup.setGroupNumber(resultSet.getInt("groupNumber"));
        nodeGroup.setUserIp(resultSet.getString("userIp"));
        nodeGroup.setTime(resultSet.getTimestamp("time"));
        return nodeGroup;
      }
    });
    return nodeGroupList;
  }
  
  public void updateTimeByUserIp(String userIp)
  {
    String sql = "update NodeGroup set time=? where userIp=?";
    Timestamp time = new Timestamp(System.currentTimeMillis());
    this.jdbcTemplate.update(sql, new Object[] { time, userIp });
  }
  
  public void setUserIpAndTimeNullByGroupNumber(int groupNumber)
  {
    String sql = "update NodeGroup set userIp=null, time=null where groupNumber=?";
    this.jdbcTemplate.update(sql, new Object[] { Integer.valueOf(groupNumber) });
  }
  
  public int getCountByUserIpNotNull()
  {
    String sql = "select count(*) from NodeGroup where userIp is not null";
    int rowCount = ((Integer)this.jdbcTemplate.queryForObject(sql, Integer.class)).intValue();
    return rowCount;
  }
  
  public List<NodeGroup> getNodeGroupByUserIpNotNull()
  {
    String sql = "select * from NodeGroup where userIp is not null";
    List<NodeGroup> nodeGroupList = this.jdbcTemplate.query(sql, new RowMapper()
    {
      public NodeGroup mapRow(ResultSet resultSet, int i)
        throws SQLException
      {
        NodeGroup nodeGroup = new NodeGroup();
        nodeGroup.setGroupNumber(resultSet.getInt("groupNumber"));
        nodeGroup.setUserIp(resultSet.getString("userIp"));
        nodeGroup.setTime(resultSet.getTimestamp("time"));
        return nodeGroup;
      }
    });
    return nodeGroupList;
  }
  
  public void setUserIpByGroupNumber(String userIp, int groupNumber)
  {
    String sql = "update NodeGroup set userIp=? where groupNumber=?";
    this.jdbcTemplate.update(sql, new Object[] { userIp, Integer.valueOf(groupNumber) });
  }
  
  public void setUserIpAndTimeByGroupNumber(String userIp, Timestamp time, int groupNumber)
  {
    String sql = "update NodeGroup set userIp=?, time=? where groupNumber=?";
    this.jdbcTemplate.update(sql, new Object[] { userIp, time, Integer.valueOf(groupNumber) });
  }
}
